package com.lms.logistics_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.logistics_management_system.common.Result;
import com.lms.logistics_management_system.entity.Announcement;
import com.lms.logistics_management_system.exception.ServiceException;
import com.lms.logistics_management_system.mapper.AnnouncementMapper;
import com.lms.logistics_management_system.service.AnnouncementService;
import com.lms.logistics_management_system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:AnnouncementController
 * Package: com.lms.logistics_management_system.controller
 * Description:
 * Author LENOVO
 * Create 2023-02-22
 * Version: v1.0
 */
// 电费支付
@RequestMapping("/announcement")
@RestController
@CrossOrigin
@Slf4j
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    @Resource
    private AnnouncementMapper announcementMapper;

    @GetMapping("/info")
    public Result getAllInfo(){
        List<Announcement> allInfo = announcementService.getAllInfo();
        return Result.success(allInfo);
    }

    @GetMapping("/page")
    public Result getPageInfo(Integer pageNum, Integer pageSize, String search, String state){
        return Result.success(announcementService.getPageAnnouncement(pageNum, pageSize));
    }

    @GetMapping("/late")
    public Result getLatestNews()
    {
        List<Announcement> latest  = announcementMapper.getLatestNews();
        return Result.success(latest);
    }

    @PostMapping
    public Result saveOrUpdateAnnouncement(@RequestBody Announcement announcement){
        boolean b = announcementService.saveOrUpdate(announcement);
        if(b)
        {
            return Result.success("保存成功");
        }
        else
        {
            return Result.error();
        }
    }

    //按前段回传的ID删除
    @DeleteMapping("/delById")
    public Result delById(@RequestParam Integer id) throws ServiceException {
        return Result.success(announcementService.removeById(id));
    }

}
